//
//  MeetingViewController.m
//  HuaRun
//
//  Created by admin on 17/8/28.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "MeetingViewController.h"
#import "MeetingTableViewCellStyle2.h"
//#import "Meeting_DetailViewController.h"
#import "Com_MeetingModel.h"
#import "Meeting_DetailViewController2.h"

@interface MeetingViewController ()

@end

@implementation MeetingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
//    "meetings_list"= "会议列表";//
    self.navigationItem.title = NSLocalizedString(@"meetings_list", nil);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.searchView.placeholderStr = NSLocalizedString(@"搜索会议名称", nil);

    [self.tableView registerClass:[MeetingTableViewCellStyle2 class] forCellReuseIdentifier:@"MeetingTableViewCellStyle2"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MeetingTableViewCellStyle2 * cell = [tableView dequeueReusableCellWithIdentifier:@"MeetingTableViewCellStyle2"
                                                                        forIndexPath:indexPath];
    cell.com_MeetingModel = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    Meeting_DetailViewController2 * detailVC = [Meeting_DetailViewController2 new];
    Com_MeetingModel * meetingModel = self.dataArray[indexPath.row];
    detailVC.meetingId = meetingModel.id;
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - method
- (void)dataInit {
    [super dataInit];
    
    [self loadData];
}

-(void)methodBind {
    
    weakself(ws);
    [self.tableView headerRefresh:^{
        [ws refreshData];
    }];
    
    self.searchView.clickBlock = ^(id result) {
        [ws searchBtnMethod];
    };
    [self.searchView.textField addTarget:self action:@selector(recoverOriginDataMethod:)
                        forControlEvents:(UIControlEventEditingChanged)];

    self.normalDZNEmptyDataSouce.reloadBlock = ^{
        [ws refreshData];
    };

}

- (void)layoutInit {
    
    weakself(ws);
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.view).offset(kMargin10);
        make.right.mas_equalTo(ws.view).offset(-kMargin10);
        make.top.mas_equalTo(ws.view).offset(0);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(ws.view);
        make.bottom.mas_equalTo(ws.view).offset(-48);
        make.top.mas_equalTo(ws.searchView.mas_bottom).offset(0);
    }];
    
}

- (void)recoverOriginDataMethod:(UITextField *)textField {
    if (textField.text.length == 0) {
        NSLog(@"恢复最初数据");
        [self loadData];
    }else {
        NSLog(@"准备搜索");
    }
}

- (void)loadData {
    [self.view endEditing:YES];
    
    NSString * keyword = [NSString isemptyStr:self.searchView.textField.text] ? @"" : self.searchView.textField.text;
    
    //    会议参数：orderBy， 取值：0  1  2  ；0代表创建日期，1代表修改日期，2代表名称
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl, kMeetingListUrl];
    NSDictionary * dic = @{@"current": @(self.current),
                           @"size": @(self.size),
                           @"name": keyword,
                           @"orderBy": @(0)
                           };
    weakself(ws);
    
    [self showActivity:kLodingStr];
    [NetWorkHelper getWithURLString:url parameters:dic success:^(id object) {
        [ws.tableView endHeaderRefresh];
        [ws hideAllAlert];
        
        if (![object[@"code"] isEqual: kSuccessCode]) {
            NSString * info = [NSString isemptyStr:object[@"msg"]] ? kUnknownError : object[@"msg"];
            [ws showWithText:info];
            return;
        }
        NSArray * array = [NSArray yy_modelArrayWithClass:[Com_MeetingModel class] json:object[@"list"]];

        ws.dataArray = ws.current == 1 ? array : [ws.dataArray arrayByAddingObjectsFromArray:array];
        [ws.tableView reloadData];
        
        if (array.count >= ws.size) {
            [ws.tableView footerRefresh:^{
                [ws loadMoreData];
            }];
        }else {
            [ws.tableView hiddenFooter];
        }
        
    } failure:^(NSError *error) {
        [ws hideAllAlert];

        //        NSLog(@"fail %@", error);
        [ws.tableView endHeaderRefresh];
        [ws showWithText:kNetworkError];
        [ws.normalDZNEmptyDataSouce setDznEmptyStatus:(DZNEmptyStatusFailure)];
 
    }];
    
}

- (void)refreshData {
    self.current = 1;
    [self loadData];
}

- (void)loadMoreData {
    self.current++;
    [self loadData];
}


//点击搜索按钮时， 页数置1， 
- (void)searchBtnMethod {
    self.current = 1;
    
    [self loadData];
}

@end
